<?php
	require_once ("./View/LoginView.php");
	require_once ("./Model/LoginHandler.php");
	require_once ("./General/page.php");
	
	define('WrongUserOrPass', 'Please check your username and/or password');
	define('LoggedOut', 'You have logged out');
	
	class LoginController{
		 
		public function DoControll(){			
			$lw = new LoginView();
			$lh = new LoginHandler();
			
			$User = $lw->GetUserName();
			$Password = $lw->GetPassword();
			
			
			
			if ($lh->IsLoggedIn() == true) {
				$page = new Page();
				$page->m_body = $lw->BannerBox() .  $lw->DoLogoutBox() . $lw->DeleteAccountLink();
				if ($lw->TriedToLogout() == true){
				$lh->DoLogout();
				$lw->DeleteCookie();
				$page->m_body = $lw->BannerBox() .  LoggedOut . $lw->DoLoginBox();
				return $page;
				}
				
				return $page;				
			}

			
			if ($lw->TriedToLogin()){
				$page = new Page();
				$page->m_title = "Labb3";			
				if ($lh->DoLoginDB($User, $Password) == true){
					if ($lw->StayLoggedIn() == true){ 
						$lw->SetOkLogin($lw->GetUserName(), $lh->GetTmpPwd($User));
					}
					$page->m_body = $lw->BannerBox() .  $lw->DoLogoutBox() . $lw->DeleteAccountLink();
					return $page;	
				}
				$page->m_body = $lw->BannerBox() . WrongUserOrPass . $lw->DoLoginBox();
				return $page;	
			}
			else {
				$page = new Page();
				$page->m_title = "Labb3";
				$page->m_body = $lw->BannerBox() . $lw->DoLoginBox();
				return $page;
			}	
		}
	}
?>